<?php
class MsglogModel extends BaseModel
{
    var $table  = 'msglog';
    var $prikey = 'id';
    var $_name  = 'msglog';

    function send_msg_system($type, $to_mobile){
        $mcode = $this->make_code();
        if ($type == 'register') {
            //注册发送短信的内容
            $smsText = "您的注册验证码是:" . $mcode . ".请不要把验证码泄露给其他人.";
        } else if ($type == 'change') {
            //修改发送的短信内容
            $smsText = "您的修改验证码是:" . $mcode . ".请不要把验证码泄露给其他人.";
        } else if ($type == 'find') {
            //找回密码发送的短信内容
            $smsText = "您的找回密码验证码是:" . $mcode . ".请不要把验证码泄露给其他人.";
        } else if ($type == 'login') {
            //找回密码发送的短信内容
            $smsText = "您的登录验证码是:" . $mcode . ".请不要把验证码泄露给其他人.";
        }
        //存入session 做认证
        unset($_SESSION['MobileConfirmCode']);
        $_SESSION['MobileConfirmCode'] = $mcode;
        $arr = $this->send_msg(0, 'admin', $to_mobile, $smsText);
        return $arr;
    }
    /**
     * 生成随机码 用于注册 以及修改
     */
    function make_code() {
        $chars = '0123456789';
        $code = '';
        for ($i = 0; $i < 4; $i++) {
            $code .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $code;
    }

    /**
     *
     * @param type $user_id   记录ID   为0 表示为系统发送消息
     * @param type $user_name  用户名
     * @param type $to_mobile  地址
     * @param type $content  内容
     * @return boolean
     */
    function send_msg($user_id, $user_name, $to_mobile, $smsText) {
        //验证是否为手机号
        if (!preg_match("/^1[123456789]\d{9}$/", $to_mobile)) {
            return $arr = array(
                'code' => -1,
                'msg'  => '手机号码格式错误'
            );
        }

        $ip = real_ip();
        if ($user_id == 0) {
            //发送验证码1分钟只能点击发送1次；
            $sql = "to_mobile = '{$to_mobile}' AND state > 0 AND time>" . (time() - 60);

            if ($this->find($sql)) {
                return $arr = array(
                    'code' => -1,
                    'msg'  => '发送验证码1分钟只能点击发送1次'
                );
            }
            //相同IP手机号码1天最多提交20次；
            $sql = "to_mobile = '{$to_mobile}' AND state > 0 AND time>" . (time() - 86400) . " AND ip='{$ip}'";
            if (count($this->find($sql)) >= 20) {
                return $arr = array(
                    'code' => -1,
                    'msg'  => '相同IP手机号码1天最多提交20次'
                );
            }
            //验证码短信单个手机号码30分钟最多提交10次；
            $sql = "to_mobile = '{$to_mobile}' AND state > 0 AND time>" . (time() - 3600);
            if (count($this->find($sql)) >= 10) {
                return $arr = array(
                    'code' => -1,
                    'msg'  => '验证码短信单个手机号码30分钟最多提交10次'
                );
            }
        }
        //发送短信
        $param=urlencode($smsText);

        $mall_config_model = &m("mall_config");
        $where1 = "code = 'msg_pid' OR code = 'msg_key'";
        $mall_config = $mall_config_model->find($where1);
        foreach($mall_config as $mc){
            if($mc['code']=='msg_pid'){
                $msg_pid=$mc['value'];
            }
            if($mc['code']=='msg_key'){
                $msg_key=$mc['value'];
            }
        }
        $url = 'http://utf8.sms.webchinese.cn/?Uid=' . $msg_pid . '&Key=' . $msg_key . '&smsMob=' . $to_mobile . '&smsText=' . $param;
        $res = request_get($url);
        $add_msglog = array(
            'user_id' => $user_id,
            'user_name' => $user_name,
            'to_mobile' => $to_mobile,
            'content' => $smsText,
            'state' => $res,
            'time' => time(),
            'ip'=>  $ip,
        );

        $this->add($add_msglog);

        if ($res > 0) {
            // user_id = 0 user_name = admin  表示为系统发送,短信的条数不做操作
            return $arr = array(
                'code' => 1,
                'msg'  => '发送成功'
            );
        } else {
            return $arr = array(
                'code' => -1,
                'msg'  => '发送失败'
            );
        }
    }
}
?>